package com.yaowk.common.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.log.Log;
import com.yaowk.common.kit.Constant;
import com.yaowk.common.kit.I18nKit;
import com.yaowk.common.kit.ResultKit;

/**
 * @authc yaowk
 * 2017/4/22
 */
public class Error500Interceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        try {
            inv.invoke();
        }
        catch (Exception e) {
            Log log = Log.getLog(Constant.COMMON_MODULE);
            if (log.isErrorEnabled()) {
                String qs = inv.getController().getRequest().getQueryString();
                String target = inv.getActionKey();
                log.error(qs == null ? target : target + "?" + qs, e);
            }
            Controller controller = inv.getController();
            controller.renderJson(ResultKit.fail(I18nKit.get("error", Constant.COMMON_MODULE, controller)));
        }
    }
}
